<template>
	<view :style="viewColor" class="wrapper">
		<view class="bag">
			<img :src="`${domain}/static/images/logo_bgh.png`" alt="" srcset="">
		</view>
		<view class="system-height" :style="{ height: statusBarHeight }"></view>

		<!-- #ifdef MP-ALIPAY -->
		<view class="title-bar" style="height: 43px;">
			<!-- <view class="icon" @click="home" >
				<text class="iconfont icon-ic_home"></text>
			</view> -->
			授权登录
		</view>
		<!-- #endif -->
		<view class="merchant-msg">
			<img :src="login_logo" />
			<view class="name">
				{{site_name}}
			</view>
		</view>
		<view class="wechat_login">
			<view class="btn-wrapper">
				<view style="display: none;">
					{{flag}}
				</view>
				<view v-if="bindPhone">
				
					<button class="bg-theme btn1" v-if="!protocol"  @click="getPhone">授权登录</button>	
					<button class="bg-theme btn1"  v-if="protocol"
					 open-type="getPhoneNumber"
					 @getphonenumber="getPhoneNumber">授权登录</button>
					</view>
				<button v-else class="bg-theme btn1" @click="getAuthLogin">
					授权登录
				</button>
			</view>
		</view>
		<view class="protocol" >
			<checkbox-group @change='ChangeIsDefault'>
				<checkbox :class="inAnimation?'trembling':''" @animationend='inAnimation=false'
					:checked="protocol ? true : false" /> <text @click='ChangeIsDefault'>已阅读并同意</text>
				<text class="main-color" @click.stop="privacy(4)">《用户协议》</text>
				与<text class="main-color" @click.stop="privacy(3)">《隐私协议》</text>
			</checkbox-group>
		</view>
		<block>
		<editUserModal :isShow="isShow" @closeEdit="closeEdit" @editSuccess="editSuccess">
		</editUserModal>
		</block>
	
	</view>
</template>

<script>

	const app = getApp();
	let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import editUserModal from '../components/eidtUserModal/index.vue'
	import privacyAgreementPopup from '../components/privacyAgreementPopup/index.vue'
	import { commonAuth } from '@/api/public';
	import { EXPIRES_TIME,	USER_INFO } from '@/config/cache';
	import { getUserInfo,loginAliPhone,getAliAuthType } from '@/api/user.js';
	import Routine from '@/libs/routine';
	import wechat from '@/libs/wechat';
	import auth from '@/libs/wechat.js';
	import { HTTP_REQUEST_URL } from '@/config/app';
	import { isWeixin,configMap } from "@/utils";
	import Cache from '@/utils/cache';
	import { toLogin } from '@/libs/login.js';
	import { mapGetters } from "vuex";
	export default {
		data() {
			return {
				domain: HTTP_REQUEST_URL,
				isUp: false,
				canClose: true,
				phone: '',
				statusBarHeight: statusBarHeight,
				isHome: false,
				isPhoneBox: false,
				protocol: false,
				isShow: false,
				logoUrl: '',
				code: '',
				codeVal: "",
				authKey: '',
				options: '',
				userInfo: {},
				codeNum: 0,
				inAnimation: false,
				colorStatus: uni.getStorageSync('color_status'),
				mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false,
				configData: Cache.get('BASIC_CONFIG'),
				bindPhone: true,
				flag: 0,
			};
		},
		computed:{
			...configMap({login_logo:'',site_name: '',first_avatar_switch: ''}, mapGetters(['isLogin','viewColor'])),
		},
		components: {
			editUserModal,
			privacyAgreementPopup
		},
		watch:{	

		},
		onLoad(options) {
		
	
			let that = this;
			let pages = getCurrentPages();
			let prePage = pages[pages.length - 2];
			if (prePage && prePage.route == 'pages/order_addcart/order_addcart') {
				this.isHome = true;
			} else {
				this.isHome = false;
			}
		},
		onReady(){
			this.getCode()
		},
		created() {
		
		},
		methods: {
			getCode() {
				let that = this
				Routine.getAliCode().then(code => {
					uni.hideLoading();
					that.code = code;
					let data = {code: code, spread: app.globalData.spid}
					getAliAuthType(data).then(res => {
						that.bindPhone = res.data.bindPhone
						if(that.bindPhone == false){
							this.$set(this, 'protocol', true);
							
						}
					}).catch(err => {
						uni.showToast({
							title: err,
							duration: 2000
						});
					});
				}).catch(e => {
					uni.hideLoading();
					uni.showToast({
						title: '登录失败',
						duration: 2000
					});
				})
				
			},
			
			getAuthLogin() {
				let self = this;
				if(!self.protocol ){
					return self.$util.Tips({
						title: '请勾选用户协议与隐私政策'
					});
				}
				uni.showLoading({
					title: "正在登录中"
				});
				let userInfo = {};
				userInfo.code = self.code;
				userInfo.spread = app.globalData.spid; //获取推广人ID
				userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
				commonAuth({
					auth: {
					type:'alipayRoutine',
					auth: userInfo
				}
				}).then(res=>{
					if(res.data.status == 200){
						let time = res.data.result.expires_time - Cache.time();
						self.$store.commit('UPDATE_USERINFO', res.data.result.user);
						self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
						self.$store.commit('SETUID', res.data.result.user.uid);
						Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
						Cache.set(USER_INFO,res.data.result.user,time);
						if(res.data.result.user.isNew && self.mp_is_new && self.first_avatar_switch==1){
							uni.hideLoading();
							self.isShow = true;
						}else{
							this.$util.Tips({
								title: '授权成功',
								icon: 'success'
							}, {
								tab: 3
							});
						}	
					}else{
						uni.setStorageSync('auth_token',res.data.result.key);
						return uni.navigateTo({
							url:'/pages/users/login/index'
						})
					}
				}).catch(res => {
					uni.hideLoading();
					console.log('auth'+res)
					uni.showToast({
						title: res,
						icon: 'none',
						duration: 2000,
					});
				});
									
			},
			onAgree() {
				this.protocol = true
			},
			ChangeIsDefault(e) {
				console.log(111)
				this.$set(this, 'protocol', !this.protocol);
				this.flag++
			},
			editSuccess() {
				this.isShow = false
			},
			closeEdit() {
				this.isShow = false
				this.$util.Tips({
					title: '登录成功',
					icon: 'success'
				}, {
					tab: 3
				});
			},
			onReject() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// #ifdef MP
			back() {
				if (this.isLogin) {
					this.$store.commit('LOGIN', {
						token: '',
						time: 0
					});
				}
				uni.navigateBack();
			},
			// #endif
			// #ifndef MP
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			// #endif
			home() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// 弹窗关闭
			maskClose(new_user) {
				this.isUp = false;
				// #ifdef MP
				if (new_user) {
					this.isShow = true
				}
				// #endif
			},
			bindPhoneClose(data) {
				this.isPhoneBox = false;
				if (data.isStatus) {
					// #ifdef MP
					this.getUserInfo(data.new_user)
					// #endif
					// #ifndef MP
					this.$util.Tips({
						title: '登录成功',
						icon: 'success'
					}, {
						tab: 3
					});
					// #endif
				}
			},
			
			
			getPhone(){
				var that =this
				if(!that.protocol){
					return that.$util.Tips({
						title: '请勾选用户协议与隐私政策'
					});
				}
			},
			// 小程序获取手机号码
			getPhoneNumber() {
				let that = this;
				if(!that.protocol){
					return that.$util.Tips({
						title: '请勾选用户协议与隐私政策'
					});
				}else{
					
					my.getPhoneNumber({
					     success: (res) => {
					       let encryptedData = res.response;
							console.log('encryptedData',encryptedData)
							if(encryptedData){
								Routine.getAliCode().then(code => {
									
									uni.hideLoading();
									that.code = code;
									loginAliPhone({
										encryptedData: encryptedData,
										code:code,
										spread: app.globalData.spid
										}).then(({
											data
										}) => {
											const backUrl = that.$Cache.get('login_back_url') || "/pages/index/index";
											that.$Cache.clear('login_back_url');
											that.$store.commit("LOGIN", {
												'token': data.token,
												'time': data.exp
											});
											that.$store.commit("SETUID", data.user.uid);
											that.$store.commit('UPDATE_USERINFO', data.user);
											let method
											let indexPat = ['/pages/index/index', '/page/my_trip/index',
												'/pages/user/index','/pages/plant_grass/index'
											]
											if (indexPat.includes(that.getPath(backUrl))) {
												method = 'switchTab'
											} else {
												method = 'navigateTo'
											}
											if (that.getPath(backUrl) === '/pages/users/login/index') {
												uni.switchTab({
													url: '/pages/index/index'
												});
												return
											}
											uni[method]({
												url: backUrl
											});
										})
										.catch(err => {
											console.log('err',err)
											that.$util.Tips({
												title: err
											});
										});
								}).catch(err => {
									console.log('err',err)
									uni.hideLoading();
									uni.showToast({
										title: '登录失败',
										duration: 2000
									});
								})
							}
					     
					     },
					     fail: (res) => {
					       console.log(res);
					     },
					});
					
					
				}
			},	
			
			getPath(url) {
				if (url.indexOf("?") != -1) {
					url = url.split("?")[0];
					console.log(url);
				}
				return url
			},
			/**
			 * 获取个人用户信息
			 */
			getUserInfo(new_user, back_url) {
				let that = this;
				getUserInfo().then(res => {
					uni.hideLoading();
					that.userInfo = res.data;
					that.$store.commit('SETUID', res.data.uid);
					that.$store.commit('UPDATE_USERINFO', res.data);
					if (new_user) {
						this.isShow = true
					} else {
						// #ifdef MP
						that.$util.Tips({
							title: '登录成功',
							icon: 'success'
						}, {
							tab: 3
						});
						// #endif
						// #ifndef MP
						that.$util.Tips({
							title: '登录成功',
							icon: 'success'
						}, {
							tab: 4,
							url: back_url || '/pages/user/index'
						});
						// #endif
					}
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: err.message,
						icon: 'none',
						duration: 2000
					});
				});
			},
			privacy(type) {
				uni.navigateTo({
					url: "/pages/users/privacy/index?type=" + type
				})
			},
			// #ifdef H5
			// 公众号登录
			wechatLogin() {
				let that = this;
				if(!this.protocol){
					return this.$util.Tips({
						title: '请勾选用户协议与隐私政策'
					});
				}else{
					auth.oAuth();
				}	
			},
			// #endif
		}
	};
</script>

<style lang="scss">
	page {
		background: #fff;
	}
	::v-deep uni-checkbox .uni-checkbox-input,
	::v-deep checkbox .wx-checkbox-input{
		border-radius: 100%;
		width: 30rpx;
		height: 30rpx;
	}
	::v-deep checkbox .uni-checkbox-input.uni-checkbox-input-checked,
	::v-deep checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		border: 2rpx solid var(--view-theme)!important;
		background-color: var(--view-theme)!important;
	}
	.wrapper {
		position: relative;
		height: 100vh;
		.bag {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			opacity: .8;
			z-index: -1;
			/* #ifdef H5 */
			z-index: 0;
			/* #endif */
			img {
				width: 100%;
				height: 838rpx;
			}
		}
		.merchant-msg {
			padding-top: 252rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			z-index: 2;
			/* #ifdef H5 */
			position: relative;
			/* #endif */
			img {
				width: 152rpx;
				height: 152rpx;
				border-radius: 50%;
			}
			.name {
				font-size: 40rpx;
				font-weight: 500;
				color: #333333;
				line-height: 56rpx;
				margin-top: 32rpx;
			}
		}
	}
	.wechat_login {
		margin-top: 96rpx;
		.img image {
			width: 100%;
		}
		.btn-wrapper {
			padding: 0 66rpx;
			button {
				width: 100%;
				height: 86rpx;
				line-height: 86rpx;
				margin-bottom: 40rpx;
				border-radius: 120rpx;
				font-size: 30rpx;
				&.btn1 {
					color: #fff;
					background: var(--view-theme);
				}
				&.btn2 {
					color: #666666;
					border: 1px solid #DDDDDD;
				}
			}
		}
	}
	.title-bar {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 34rpx;
		font-weight: 500;
		color: #333333;
		line-height: 48rpx;
	}
	.icon {
		position: absolute;
		left: 30rpx;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 80rpx;
		height: 80rpx;
		image {
			width: 50rpx;
			height: 50rpx;
		}
	}
	.protocol {
		position: fixed;
		bottom: 52rpx;
		left: 0;
		width: 100%;
		margin: 0 auto;
		color: #999999;
		font-size: 24rpx;
		line-height: 22rpx;
		text-align: center;
		bottom: calc(52rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		bottom: calc(52rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		.main-color {
			color: var(--view-theme);
		}
		.trembling {
			animation: shake 0.6s;
		}
	}
</style>